#include "my_call.hpp"
#include "pjsip_manager.hpp"
#include <iostream>

#define THIS_FILE "my_call.cpp"

using namespace pj;

void MyCall::addWavpath(const string &file_name)
{
    wav_file_path = file_name;
}

void MyCall::onCallState(OnCallStateParam &prm)
{
    try
    {
        CallInfo info = getInfo();
        if (info.state == PJSIP_INV_STATE_DISCONNECTED)
        {
            stopPlayer();
        }

        PJSUAManager::getInstace()->onCallStateChange(info);
    }
    catch (Error &e)
    {
        std::cout << e.info() << std::endl;
        std::cout << e.reason << std::endl;
        return;
    }
}

void MyCall::onCallMediaState(OnCallMediaStateParam &prm)
{
    CallInfo info;
    try
    {
        info = getInfo();
    }
    catch (Error &e)
    {
        std::cout << e.info() << std::endl;
        std::cout << e.reason << std::endl;
        return;
    }

    for (unsigned i = 0; i < info.media.size(); i++)
    {
        CallMediaInfo med_info = info.media[i];
        PJ_LOG(4, (THIS_FILE, "media type is: %d, status is: %d, dir is: %d", med_info.type, med_info.status, med_info.dir));
        if ((med_info.type == PJMEDIA_TYPE_AUDIO) &&
            ((med_info.status == PJSUA_CALL_MEDIA_ACTIVE) ||
             (med_info.status == PJSUA_CALL_MEDIA_REMOTE_HOLD)))
        {
            // 临时测试
            if (med_info.dir == PJMEDIA_DIR_NONE)
                continue;

            Media *m = getMedia(i);
            AudioMedia *am = AudioMedia::typecastFromMedia(m);
            try
            {
                am->adjustRxLevel(2.0);
                PJSUAManager::getInstace()->adjustVoidLevel(2.0);
            }
            catch (Error &e)
            {
                std::cout << e.info() << std::endl;
                std::cout << e.reason << std::endl;
            }

            AudDevManager &aud_mgr =
                Endpoint::instance().audDevManager();

            if (info.state == PJSIP_INV_STATE_CONNECTING && !wav_file_path.empty() && !wav_player)
            {
                wav_player = new AudioMediaPlayer();
                try
                {
                    wav_player->createPlayer(wav_file_path, PJMEDIA_FILE_NO_LOOP);
                }
                catch (Error &e)
                {
                    std::cout << e.info() << std::endl;
                    delete wav_player;
                    wav_player = NULL;
                }

                if (wav_player)
                {
                    wav_player->startTransmit(*am);
                    wav_player->startTransmit(aud_mgr.getPlaybackDevMedia());
                }
            }

            try
            {
                aud_mgr.getCaptureDevMedia().startTransmit(*am);
                am->startTransmit(aud_mgr.getPlaybackDevMedia());
                current_am = am;
            }
            catch (Error &e)
            {
                std::cout << e.info() << std::endl;
                std::cout << e.reason << std::endl;
                continue;
            }
        }
    }
}

void MyCall::stopPlayer()
{
    if (!wav_player || !current_am)
        return;
    AudDevManager &aud_mgr =
        Endpoint::instance().audDevManager();
    try
    {
        CallInfo info = getInfo();
        if (info.state != PJSIP_INV_STATE_DISCONNECTED)
        {
            wav_player->stopTransmit(*current_am);
        }

        wav_player->stopTransmit(aud_mgr.getPlaybackDevMedia());
        delete wav_player;
        wav_player = NULL;
    }
    catch (Error &e)
    {
        delete wav_player;
        wav_player = NULL;
        std::cout << e.info() << std::endl;
        std::cout << e.reason << std::endl;
    }

}